package be.jabapage.util;

/**
 * 
 */

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 
 * @author Jan Verstuyft
 */
public class MemoryByteArrayOutputStream extends OutputStream {
	private final List<Byte> bufferList;


	/**
	 * Create a new character-stream writer whose critical sections will
	 * synchronize on the writer itself.
	 */
	public MemoryByteArrayOutputStream(final List<Byte> bufferList) {
		this.bufferList = bufferList;
	}

	/**
	 * Write a portion of an array of characters.
	 * 
	 * @param cbuf
	 *            Array of characters
	 * @param off
	 *            Offset from which to start writing characters
	 * @param len
	 *            Number of characters to write
	 * @throws java.io.IOException
	 *             If an I/O error occurs
	 */
	@Override
	public synchronized void write(final byte[] cbuf, final int off,
			final int len) throws IOException {
		if (len < 0) {
			throw new IllegalArgumentException();
		}
		if (off < 0) {
			throw new IndexOutOfBoundsException();
		}
		if (cbuf == null) {
			throw new NullPointerException();
		}
		if ((len + off) > cbuf.length) {
			throw new IndexOutOfBoundsException();
		}

		for (int i = off; i < off + len; i++) {
			bufferList.add(cbuf[i]);
		}
	}

	/**
	 * Writes <code>b.length</code> bytes from the specified byte array to this
	 * output stream. The general contract for <code>write(b)</code> is that it
	 * should have exactly the same effect as the call <code>write(b, 0,
	 * b.length)</code>.
	 * 
	 * @param b
	 *            the data.
	 * @throws java.io.IOException
	 *             if an I/O error occurs.
	 * @see java.io.OutputStream#write(byte[], int, int)
	 */
	@Override
	public void write(final byte[] b) throws IOException {
		write(b, 0, b.length);
	}

	/**
	 * Writes the specified byte to this output stream. The general contract for
	 * <code>write</code> is that one byte is written to the output stream. The
	 * byte to be written is the eight low-order bits of the argument
	 * <code>b</code>. The 24 high-order bits of <code>b</code> are ignored.
	 * <p/>
	 * Subclasses of <code>OutputStream</code> must provide an implementation
	 * for this method.
	 * 
	 * @param b
	 *            the <code>byte</code>.
	 * @throws java.io.IOException
	 *             if an I/O error occurs. In particular, an
	 *             <code>IOException</code> may be thrown if the output stream
	 *             has been closed.
	 */
	@Override
	public synchronized void write(final int b) throws IOException {
		byte[] singleIntArray = new byte[1];
		singleIntArray[0] = (byte) (0xFF & b);
		write(singleIntArray, 0, 1);
	}

	/**
	 * Flush the stream. If the stream has saved any characters from the various
	 * write() methods in a buffer, write them immediately to their intended
	 * destination. Then, if that destination is another character or byte
	 * stream, flush it. Thus one flush() invocation will flush all the buffers
	 * in a chain of Writers and OutputStreams.
	 * <p/>
	 * If the intended destination of this stream is an abstraction provided by
	 * the underlying operating system, for example a file, then flushing the
	 * stream guarantees only that bytes previously written to the stream are
	 * passed to the operating system for writing; it does not guarantee that
	 * they are actually written to a physical device such as a disk drive.
	 * 
	 * @throws java.io.IOException
	 *             If an I/O error occurs
	 */
	@Override
	public void flush() throws IOException {
	}

	/**
	 * Close the stream, flushing it first. Once a stream has been closed,
	 * further write() or flush() invocations will cause an IOException to be
	 * thrown. Closing a previously-closed stream, however, has no effect.
	 * 
	 * @throws java.io.IOException
	 *             If an I/O error occurs
	 */
	@Override
	public void close() throws IOException {
	}

	public synchronized byte[] toByteArray() {
		final byte[] retval = new byte[bufferList.size()];
		int i = 0;
		for (Byte b : bufferList) {
			retval[i++] = b.byteValue();
		}
		return retval;
	}

	public int getLength() {
		return bufferList.size();
	}

	public byte[] getRaw() {
		return toByteArray();
	}
}